package com.monster.servlet;

import com.alibaba.fastjson.JSONArray;
import com.monster.pojo.Album;
import com.monster.pojo.Playlist;
import com.monster.pojo.Singer;
import com.monster.pojo.Song;
import com.monster.service.AlbumService;
import com.monster.service.ListServiceImpl;
import com.monster.service.PlaylistService;
import com.monster.service.SingerService;
import com.monster.service.SongService;
import com.monster.service.SongServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/SearchAllServlet")
public class SearchAllServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opr = request.getParameter("opr");
        switch (opr){
            case "searchSingers":
                searchSingers(request,response);
                break;
            case "searchSinger":
                searchSinger(request,response);
                break;
            case "searchSongs":
                searchSongs(request,response);
                break;
            case "searchAlbums":
                searchAlbums(request,response);
                break;
            case "searchPlaylists":
                searchPlaylists(request,response);
                break;
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
//直接进来
    private void searchSingers(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        SingerService singerService = new ListServiceImpl();
        String text = request.getParameter("content");
        ArrayList<Singer> singerList = new ArrayList<>();
        singerList = singerService.getSingersByFuzzy(text);
        /*for(Singer sin:singerList){
            System.out.println(sin.getSingerName()+"\t"+sin.getSingerId());
        }*/
        if(singerList!=null){
            request.setAttribute("text",text);
            request.setAttribute("singerList",singerList);
            try {
                request.getRequestDispatcher("search.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            request.setAttribute("text",text);
            try {
                request.getRequestDispatcher("search.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //歌手
    private void searchSinger(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        SingerService singerService = new ListServiceImpl();
        String str = request.getParameter("hot_singers");
        String text=new String(str.getBytes("ISO-8859-1"),"utf-8");
        ArrayList<Singer> singerList = new ArrayList<>();
        singerList = singerService.getSingersByFuzzy(text);
        for(Singer sin:singerList){
            System.out.println(sin.getSingerName()+"\t"+sin.getSingerId());
        }
        if(singerList!=null){
            request.setAttribute("text",text);
            request.setAttribute("singerList",singerList);
            try {
                request.getRequestDispatcher("search.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            request.setAttribute("text",text);
            try {
                request.getRequestDispatcher("search.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
//歌曲
    private void searchSongs(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        SongService songService = new SongServiceImpl();
        String str = request.getParameter("hot_songs");
        String text=new String(str.getBytes("ISO-8859-1"),"utf-8");
        System.out.println(text);
        ArrayList<Song> songList = new ArrayList<>();
        songList = songService.listSongsByFuzzy(text);
        if(songList!=null){
            request.setAttribute("text",text);
            request.setAttribute("songList",songList);
            try {
                request.getRequestDispatcher("search.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            request.setAttribute("text",text);
            try {
                request.getRequestDispatcher("search.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

//专辑
    private void searchAlbums(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        AlbumService albumService = new ListServiceImpl();
        String str = request.getParameter("hot_albums");
        String text=new String(str.getBytes("ISO-8859-1"),"utf-8");
        ArrayList<Album> albumsList = new ArrayList<>();
        albumsList = albumService.findAlbumbyAlbumname(text);
        if(albumsList!=null){
            request.setAttribute("text",text);
            request.setAttribute("albumsList",albumsList);
            try {
                request.getRequestDispatcher("search.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            request.setAttribute("text",text);
            try {
                request.getRequestDispatcher("search.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
//歌单
    private void searchPlaylists(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PlaylistService playlistService = new ListServiceImpl();
        String str = request.getParameter("hot_playlists");
        String text=new String(str.getBytes("ISO-8859-1"),"utf-8");
        List<Playlist> playlistsList = new ArrayList<>();
        playlistsList = playlistService.selectPlaylist(text);
        if(playlistsList!=null){
            request.setAttribute("text",text);
            request.setAttribute("playlistsList",playlistsList);
            try {
                request.getRequestDispatcher("search.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            request.setAttribute("text",text);
            try {
                request.getRequestDispatcher("search.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
